import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../layout/Home.vue'
import local from '@/utils/local.js'

// 获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home,
    redirect: './dashboard',
    children: [
      // 后台首页
      {
        path: '/dashboard',
        component: () =>
          import(/* webpackChunkName: "about" */ '../views/dashboard/index.vue')
      }
    ]
  },
  {
    path: '/login',
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/Login.vue')
  },
  {
    path: '*',
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/ErrorPage/index.vue'),
    redirect: '/404',
    children: [
      {
        path: '/404',
        component: () =>
          import(/* webpackChunkName: "about" */ '../views/ErrorPage/index.vue')
      }
    ]
  }
]

// component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
// 1. 分离路由
const SyncRouter = {
  path: '/home',
  component: Home,
  redirect: './dashboard',
  children: [
    // 后台首页
    {
      path: '/dashboard',
      isVisible: 'true',
      meta: { name: '后台首页' },
      component: () =>
        import(/* webpackChunkName: "about" */ '../views/dashboard/index.vue')
    },
    // 订单管理
    {
      path: '/order',
      isVisible: 'true',
      meta: { name: '订单管理', role: ['super', 'normal'] },
      redirect: '/order/list',
      component: () =>
        import(/* webpackChunkName: "about" */ '../views/orders/index.vue'),

      children: [
        // 订单列表
        {
          path: '/order/list',
          isVisible: 'true',
          meta: { name: '商品列表' },
          component: () =>
            import(/* webpackChunkName: "about" */ '../views/orders/list.vue')
        },
        {
          path: '/order/edit',
          isVisible: 'false',
          meta: { name: '商品编辑', role: ['super'] },
          component: () =>
            import(/* webpackChunkName: "about" */ '../views/orders/edit.vue')
        }
      ]
    },
    // 商品管理
    {
      path: '/goods',
      isVisible: 'true',
      meta: { name: '商品管理', role: ['super', 'normal'] },
      component: () =>
        import(/* webpackChunkName: "about" */ '../views/goods/index.vue'),
      redirect: '/goods/list',
      children: [
        // 商品列表
        {
          path: '/goods/list',
          isVisible: 'true',
          meta: { name: '商品列表' },
          component: () =>
            import(/* webpackChunkName: "about" */ '../views/goods/list.vue')
        },
        // 商品添加
        {
          path: '/goods/add',
          isVisible: 'true',
          meta: { name: '添加商品', role: ['super'] },
          component: () =>
            import(/* webpackChunkName: "about" */ '../views/goods/add.vue')
        },
        // 商品分类
        {
          path: '/goods/cate',
          isVisible: 'true',
          meta: { name: '商品分类' },
          component: () =>
            import(/* webpackChunkName: "about" */ '../views/goods/cate.vue')
        }
      ]
    },
    // 店铺管理
    {
      path: '/store',
      isVisible: 'true',
      meta: { name: '店铺管理', role: ['super'] },
      component: () =>
        import(/* webpackChunkName: "about" */ '../views/store/index.vue')
    },
    // 账号管理
    {
      path: '/account',
      isVisible: 'true',
      meta: { name: '账号管理', role: ['super', 'normal'] },
      redirect: './account/list',
      component: () =>
        import(/* webpackChunkName: "about" */ '../views/account/index.vue'),
      children: [
        // 账号列表
        {
          path: '/account/list',
          isVisible: 'true',
          meta: { name: '账号列表', role: ['super'] },
          component: () =>
            import(/* webpackChunkName: "about" */ '../views/account/list.vue')
        },
        // 添加账号
        {
          path: '/account/add',
          isVisible: 'true',
          meta: { name: '添加账号', role: ['super'] },
          component: () =>
            import(/* webpackChunkName: "about" */ '../views/account/add.vue')
        },
        // 修改密码
        {
          path: '/account/reset',
          isVisible: 'true',
          meta: { name: '修改密码' },
          component: () =>
            import(/* webpackChunkName: "about" */ '../views/account/reset.vue')
        },
        // 个人中心
        {
          path: '/account/person',
          isVisible: 'false',
          meta: { name: '个人中心' },
          component: () =>
            import(
              /* webpackChunkName: "about" */ '../views/account/personal.vue'
            )
        }
      ]
    },
    // 销售统计
    {
      path: '/stats',
      isVisible: 'true',
      meta: { name: '销售统计', role: ['super'] },
      component: () =>
        import(/* webpackChunkName: "about" */ '../views/stats/index.vue'),
      redirect: './stats/pro',
      children: [
        // 商品统计
        {
          path: '/stats/pro',
          isVisible: 'true',
          meta: { name: '商品统计' },
          component: () =>
            import(/* webpackChunkName: "about" */ '../views/stats/pro.vue')
        },
        // 订单统计
        {
          path: '/stats/order',
          isVisible: 'true',
          meta: { name: '订单统计' },
          component: () =>
            import(/* webpackChunkName: "about" */ '../views/stats/order.vue')
        }
      ]
    }
  ]
}

const router = new VueRouter({
  routes
})
// 2. 声明一个函数,对上面分离的路由进行计算
calcRouter()
// 防止没有路由的情况
export function calcRouter() {
  const role = local.get('role')
  if (!role) return
  // 2.1 获取到最终过滤出来的路由
  const finallyRouter = filterFn(SyncRouter.children, role)
  // 添加路由必须是一个数组 用finallyRouter替换SyncRouter.children
  SyncRouter.children = finallyRouter
  console.log(finallyRouter)
  // 2.2 合并到主路由上
  router.addRoutes([SyncRouter])
  // 2.3 把计算出来的数组通过 isvisible 过滤出来
  const NavMenu = caclMenu(finallyRouter)
  local.set('showMenu', JSON.stringify(NavMenu))
}
function caclMenu(menu) {
  return menu.filter(item => {
    if (item.isVisible) {
      if (item.children && item.children.length > 0) {
        item.children = caclMenu(item.children)
      }
      return true
    } else {
      return false
    }
  })
}

// 3. 主要做功能的函数
function filterFn(router, role) {
  // 把满足条件的留下,放入一个新数组中
  return router.filter(item => {
    // 需要鉴权
    if (hasPermission(item, role)) {
      if (item.children && item.children.length > 0) {
        item.children = filterFn(item.children, role)
      }
      return true
    } else {
      return false
    }
  })
}

// 4. 鉴权函数,判断当前路由是否有权限
function hasPermission(item, r) {
  // 对比权限
  if (item.meta && item.meta.role) {
    return item.meta.role.includes(r)
  } else {
    return true
  }
}

// 导航守卫,目的是不允许未登录的用户进入路由页面
router.beforeEach((to, from, next) => {
  const token = local.get('token')
  if (token) {
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
